clear all

global user "~\SHEPad"

global output "~\SHEPad\output"
cd "${output}"

* year-interacted controls
global yearinteracted
global controls "total_enrolbg_2013 bookinlib_2013 toiletd_2013 toiletb_2013 toiletg_2013 clrooms_2013 clgood_2013 smc_2013 smcsdp_2013 medchk_2013 electric_2013 library_2013 playground_2013 ramps_2013 cal_2013 access_2013 water_hp_2013 water_well_2013 water_tap_2013 water_others_2013 bld_govt_2013 bld_uc_2013 bld_dilap_2013 wall_pucca_2013 wall_wire_2013 wall_hedges_2013 wall_na_2013"
foreach v in $controls {
  global yearinteracted $yearinteracted c.`v'#i.year
}

* label variable
cap prog drop labelvar
program define labelvar, rclass
label var total_enrol_2013 "Total Girls’ Enrollment"
label var total_enrolbg_2013 "Total Student Enrollment"
label var clrooms_2013 "No. of classrooms"
label var clgood_2013 "No. of classrooms in good condition"
label var toiletb_2013 "No. of boys’ toilets"
label var toiletg_2013 "No. of girls’ toilets"
label var toiletd_2013 "No. of toilets for children with special needs"
label var bookinlib_2013 "No. of books in library"
label var electric_2013 "Presence of Electricity"
label var library_2013 "Presence of Library"
label var playground_2013 "Presence of Playground"
label var ramps_2013 "Presence of Ramps"
label var medchk_2013 "Presence of Regular Medical Check-ups"
label var cal_2013 "Presence of Computer Lab"
label var smc_2013 "Presence of School Management Committee"
label var smcsdp_2013 "Presence of School Development Plan"
label var access_2013 "Access by Road"
label var water_hp_2013 "Water Source: Hand Pump"
label var water_well_2013 "Water Source: Well"
label var water_tap_2013 "Water Source: Tap"
label var water_others_2013 "Water Source: Others"
label var bld_govt_2013 "Building: Government-owned"
label var bld_uc_2013 "Building: Under Construction"
label var bld_dilap_2013 "Building: Dilapidated"
label var wall_pucca_2013 "Wall: Brick"
label var wall_wire_2013 "Wall: Barbed Wire Fencing"
label var wall_hedges_2013 "Wall: Hedges"
label var wall_na_2013 "Wall: None"
end

* count number of schools
cap prog drop tabschool
program define tabschool, rclass
preserve
keep if e(sample)
duplicates drop schcd, force
tab t_machine_installed
restore
end


********************************************************************************
*                                 Appendix A1                                  *
********************************************************************************

use "${user}\data\datause.dta", clear
keep if caste=="TOTAL" & t_machine_installed==1 & gender==2
preserve
duplicates drop schcd, force
graph bar (sum) t_machine_installed,over(t_machine_year) graphregion(color(white) margin(2 2 2 2)) b1title("Year") ytitle(Number of Treated Schools) bar(1,color(black%60)) ylabel(, nogrid) plotregion(lcolor(black)) lintensity(0) 
restore

keep if year==2013 & (schtype==2 | schtype==3)
egen c_upper=rowtotal(c7 c8 c9 c10 c11 c12)
twoway scatter c_upper averagenumberofstudentsutilising if rural==0, mfcolor(black%60) mlwidth(none) msymbol(triangle) || scatter c_upper averagenumberofstudentsutilising if rural==1, ytitle("Total Number of Girls' Enrollment from 7th-grade") xtitle("Average Number of Students Utilising the Machine per Month") msize(small) graphregion(color(white) margin(2 2 2 2)) mfcolor(black%60) mlwidth(none) ylabel(, nogrid) legend(order(1 "Urban School" 2 "Rural School") nobox region(lstyle(none)))
gen ratio=averagenumberofstudentsutilising/(c_upper*0.8)
bysort rural: summ ratio, detail

tab lowclass highclass, cell

********************************************************************************
*                                 Appendix A2                                  *
********************************************************************************

use "${user}\data\datause.dta", clear
keep if caste=="TOTAL" & (schtype==2 | schtype==3) & gender==2 & year==2013

global svar "total_enrol_2013 total_enrolbg_2013 clrooms_2013 clgood_2013 toiletb_2013 toiletg_2013 toiletd_2013 bookinlib_2013 electric_2013 library_2013 playground_2013 ramps_2013 medchk_2013 cal_2013 smc_2013 smcsdp_2013  access_2013 water_hp_2013 water_well_2013 water_tap_2013 water_others_2013 bld_govt_2013 bld_uc_2013 bld_dilap_2013 wall_pucca_2013 wall_wire_2013 wall_hedges_2013 wall_na_2013"
eststo full: quietly estpost summarize $svar
eststo treat: quietly estpost summarize $svar if t_machine_installed==1
eststo control: quietly estpost summarize $svar if t_machine_installed==0
eststo diff: quietly estpost ttest $svar, by(t_machine_installed) unequal
labelvar

esttab full treat control diff using "${output}\AppendixA2.csv", replace cells("mean(pattern(1 1 1 0) fmt(3)) b(star pattern(0 0 0 1) fmt(3))") label mtitles("Full Sample" "Treatment Group" "Control Group" "Mean Differences (3-2")

********************************************************************************
*                                 Appendix A3                                  *
********************************************************************************

use "${user}\data\data.dta", clear

reghdfe chg_enrolment7 aft $yearinteracted if balancedpanel7==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
keep if e(sample) & year==2013

global svar "total_enrol_2013 total_enrolbg_2013 clrooms_2013 clgood_2013 toiletb_2013 toiletg_2013 toiletd_2013 bookinlib_2013 electric_2013 library_2013 playground_2013 ramps_2013 medchk_2013 cal_2013 smc_2013 smcsdp_2013  access_2013 water_hp_2013 water_well_2013 water_tap_2013 water_others_2013 bld_govt_2013 bld_uc_2013 bld_dilap_2013 wall_pucca_2013 wall_wire_2013 wall_hedges_2013 wall_na_2013"
eststo treat: quietly estpost summarize $svar if t_machine_installed==1
eststo control: quietly estpost summarize $svar if t_machine_installed==0
eststo diff: quietly estpost ttest $svar, by(t_machine_installed) unequal
labelvar

esttab treat control diff using "${output}\AppendixA3.csv", replace cells("mean(pattern(1 1 0) fmt(3)) b(star pattern(0 0 1) fmt(3))") label mtitles("Treatment Group" "Control Group" "Mean Differences (2-1)")

********************************************************************************
*                                 Appendix A4                                  *
********************************************************************************

use "${user}\data\data.dta", clear

forval c=1/11 {
egen totalc`c'=total(c`c'), missing by(blockcode year)
egen totalc`c'_next=total(c`c'_next), missing by(blockcode year)
}
egen totalc=rowtotal(totalc1 totalc2 totalc3 totalc4 totalc5 totalc6 totalc7 totalc8 totalc9 totalc10 totalc11)
replace totalc=. if totalc==0
egen totalc_next=rowtotal(totalc1_next totalc2_next totalc3_next totalc4_next totalc5_next totalc6_next totalc7_next totalc8_next totalc9_next totalc10_next totalc11_next)
replace totalc_next=. if totalc_next==0
gen diff = ((totalc-totalc_next)/totalc)*100 if !missing(totalc) & !missing(totalc_next)
egen mint_machine_year=min(t_machine_year), by(blockcode year)
egen mint_machine_installed=max(t_machine_installed), by(blockcode year)

keep year blockcode year totalc totalc_next mint_machine_year mint_machine_installed diff
duplicates drop blockcode year, force
gen treatment_period=year-mint_machine_year if !missing(mint_machine_year)
keep if mint_machine_year!=.
gen after=treatment_period>=0

graph bar (mean) diff, over(after, relabel(1 "before treatment" 2 "after treatment")) graphregion(color(white)) bar(1, color(black%80) lcolor(black%80)) ytitle(Average Dropout Rate) saving("${output}\AppendixA4.gph", replace)

********************************************************************************
*                                 Appendix A5                                  *
********************************************************************************

use "${user}\data\data.dta", clear

* label variable
label var aft AfterT

reghdfe chg_enrolment5 aft $yearinteracted, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA5.doc", replace nocons nonotes label bdec(3) sdec(3) ctitle(5th-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

reghdfe chg_enrolment6 aft $yearinteracted, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA5.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(6th-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

reghdfe chg_enrolment7 aft $yearinteracted, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA5.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(7th-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

reghdfe chg_enrolment8 aft $yearinteracted, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA5.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(8th-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

reghdfe chg_enrolment9 aft $yearinteracted, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA5.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(9th-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

reghdfe chg_enrolment10 aft $yearinteracted, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA5.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(10th-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

********************************************************************************
*                                 Appendix A6                                  *
********************************************************************************

* define matching types
cap prog drop match1 match2 match3 match4 match5
program define match1, rclass
psmatch2 t_machine_installed $controls if balancedpanel7==1, qui outcome(chg_enrolment7) n(1) trim(2)
end
program define match2, rclass
psmatch2 t_machine_installed $controls if balancedpanel7==1, qui outcome(chg_enrolment7) n(10) trim(2) ties
end
program define match3, rclass
psmatch2 t_machine_installed $controls if balancedpanel7==1, qui outcome(chg_enrolment7) trim(2) kernel kerneltype(epan)
end
program define match4, rclass
psmatch2 t_machine_installed $controls if balancedpanel7==1, qui outcome(chg_enrolment7) trim(2) radius caliper(0.10)
end
program define match5, rclass
psmatch2 t_machine_installed if balancedpanel7==1, mahalanobis($controls) outcome(chg_enrolment7)
end


* PSM
set seed 1234
forval i=1/5 {
use "${user}\data\data.dta", clear
* Step 1: match control to treated schools
preserve
keep if balancedpanel7==1 & year==2013
duplicates drop schcd, force
qui match`i'
keep schcd _weight
keep if _weight>0 & !missing(_weight)
save "${user}\data\psmatch`i'.dta",replace
restore

* Step 2: run regression using matched sample
preserve
merge m:1 schcd using "${user}\data\psmatch`i'.dta", keepusing(_weight) keep(3) nogen
statsby _b[aft], saving("${user}\data\coef_psm`i'.dta",replace): reghdfe chg_enrolment7 aft if balancedpanel7==1, absorb(school_gender##c.year blockyear)
restore

* Step 3: repeat regression 1000 times using random matched samples to calculate standard errors from regression coefficient
forval j=1/1050{
use "${user}\data\data.dta", clear
keep if balancedpanel7==1

* generate random sample by schcd cluster
bsample, cluster(schcd) idcluster(schcd_gender)
save "${user}\data\pssample`i'.dta",replace
keep if year==2013
duplicates drop schcd, force
capture nois match`i'

if _rc ==0 {
keep schcd _weight
keep if _weight>0 & !missing(_weight)
duplicates drop schcd, force
save "${user}\data\psmatch`i'.dta",replace

* run regression using psmatch sample
use "${user}\data\pssample`i'.dta",clear
merge m:1 schcd using "${user}\data\psmatch`i'.dta"
statsby _b[aft], saving("${user}\data\psm`i'.dta",replace): reghdfe chg_enrolment7 aft if _m==3 & balancedpanel7==1, absorb(schcd_gender##c.year blockyear)

* append coefficients
use "${user}\data\coef_psm`i'.dta",clear
append using "${user}\data\psm`i'.dta"
save "${user}\data\coef_psm`i'.dta",replace
}
}

*calculate bootstrap standard error from coefficients
preserve
use "${user}\data\coef_psm`i'.dta", clear
keep in 2/1001
egen meanb=mean(_stat_1)
gen diff2=(_stat_1-meanb)^2
collapse (sum) diff2
gen s=((1/(1000-1))*diff2)^1/2
list s
restore
}

* CEM
set seed 1234
* Step 1: match control to treated schools
use "${user}\data\data.dta", clear
preserve
keep if balancedpanel7==1 & year==2013
duplicates drop schcd, force
qui cem $controls if balancedpanel7==1, treatment(t_machine_installed)
keep schcd cem_matched
keep if cem_matched==1
save "${user}\data\psmatch.dta",replace
restore

* Step 2: run regression using matched sample
preserve
merge m:1 schcd using "${user}\data\psmatch.dta", keep(3) nogen
statsby _b[aft], saving("${user}\data\coef_psm.dta",replace): reghdfe chg_enrolment7 aft if balancedpanel7==1, absorb(school_gender##c.year blockyear)
restore

* Step 3: repeat regression 1000 times using random matched samples to calculate standard errors from regression coefficient
forval i=1/1050{
use "${user}\data\data.dta", clear
* generate random sample by schcd cluster
bsample, cluster(schcd) idcluster(schcd_gender)
save "${user}\data\pssample.dta",replace
keep if year==2013
duplicates drop schcd, force
capture nois cem $controls if balancedpanel7==1, treatment(t_machine_installed)

if _rc ==0 {
keep schcd cem_matched
keep if cem_matched==1
duplicates drop schcd, force
save "${user}\data\psmatch.dta",replace

* run regression using psmatch sample
use "${user}\data\pssample.dta",clear
merge m:1 schcd using "${user}\data\psmatch.dta"
statsby _b[aft], saving("${user}\data\psm.dta",replace): reghdfe chg_enrolment7 aft if _m==3 & balancedpanel7==1, absorb(schcd_gender##c.year blockyear)

* append coefficients
use "${user}\data\coef_psm.dta",clear
append using "${user}\data\psm.dta"
save "${user}\data\coef_psm.dta",replace
}
}

*calculate bootstrap standard error from coefficients
preserve
use "${user}\data\coef_psm.dta", clear
keep in 2/1001
egen meanb=mean(_stat_1)
gen diff2=(_stat_1-meanb)^2
collapse (sum) diff2
gen s=((1/(1000-1))*diff2)^1/2
list s
restore

********************************************************************************
*                                 Appendix A7                                  *
********************************************************************************

* PSM
forval i=1/5 {
use "${user}\data\data.dta", clear

preserve
keep if balancedpanel7==1 & year==2013
duplicates drop schcd, force
qui match`i'
keep schcd _weight
keep if _weight>0 & !missing(_weight)
save "${user}\data\psmatch`i'.dta",replace
restore

preserve
merge m:1 schcd using "${user}\data\psmatch`i'.dta", keep(3) nogen
reghdfe chg_enrolment7 aft if balancedpanel7==1, absorb(school_gender##c.year blockyear)
keep if e(sample) & year==2013
eststo diff`i': quietly estpost ttest $svar, by(t_machine_installed) unequal
restore
}

* CEM
set seed 1234
use "${user}\data\data.dta", clear
preserve
keep if balancedpanel7==1 & year==2013
duplicates drop schcd, force
qui cem $controls if balancedpanel7==1, treatment(t_machine_installed)
keep schcd cem_matched
keep if cem_matched==1
save "${user}\data\psmatch.dta",replace
restore

preserve
merge m:1 schcd using "${user}\data\psmatch.dta", keep(3) nogen
reghdfe chg_enrolment7 aft if balancedpanel7==1, absorb(school_gender##c.year blockyear)
keep if e(sample) & year==2013
eststo diff: quietly estpost ttest $svar, by(t_machine_installed) unequal
restore

esttab diff1 diff2 diff3 diff4 diff5 diff using "${output}\AppendixA7.csv", replace cells("b(star pattern(1 1 1 1 1 1) fmt(3))") label

********************************************************************************
*                                 Appendix A8                                  *
********************************************************************************

use "${user}\data\data.dta", clear

forval i=1/11 {
gen log_c`i'=log(c`i'+1) if !missing(c`i')
}
forval i = 6/11 {
local j=`i'-1
reghdfe log_c`i' before6 before5 before4 before3 before2 after0 after1 after2 after3 after4 before1 $yearinteracted if balancedpanel`j'==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
est store Dynamic`i'
coefplot Dynamic`i', omitted keep(before* after*) order(before7 before6 before5 before4 before3 before2  before1 after0 after1 after2 after3) vertical recast(connect) ciopts(recast(rcap) lpattern(dash) lcolor(black%90)) lpattern(solid) mcolor(black) lcolor(black) yline(0,lcolor(gray%50)) xtitle(Treatment Period) ylabel(-.5(.1).5,angle(90) glcolor(gs15%50)) scale(titlegap(.5) outergap(.5)) graphregion(color(white) margin(2 2 2 2)) plotregion(lcolor(black)) xline(6, lcolor(red%50)) title(`i'th Grade ) ytitle(DID Estimates)
graph save "${output}\figure_a`i'.gph",replace
}
graph combine figure_a6.gph figure_a7.gph figure_a8.gph figure_a9.gph figure_a10.gph figure_a11.gph, row(2) col(3) ysize(10) xsize(20) commonscheme graphregion(color(white)) saving("${output}\AppendixA8.gph", replace)

forval i=6/11 {
erase "${output}\figure_a`i'.gph"
}

********************************************************************************
*                                 Appendix A9                                  *
********************************************************************************

use "${user}\data\data.dta", clear

egen maxtreat = max(t_machine_installed), by(villagecode)
drop if t_machine_installed==0 & maxtreat==1
reghdfe chg_enrolment7 before6 before5 before4 before3 before2 after0 after1 after2 after3 before1 $yearinteracted if balancedpanel7==1 , absorb(i.school_gender##c.year blockyear) vce(cluster schcd)
est store d7
coefplot d7, omitted keep(before* after*) order(before6 before5 before4 before3 before2  before1 after0 after1 after2 after3) vertical recast(connect) ciopts(recast(rcap) lpattern(dash) lcolor(black%90)) lpattern(solid) mcolor(black) lcolor(black) yline(0,lcolor(gray%50)) xtitle(Treatment Period) ylabel(-1.5(.5)2,angle(90) glcolor(gs15%50)) scale(titlegap(.5) outergap(.5)) graphregion(color(white) margin(2 2 2 2)) plotregion(lcolor(black)) xline(6, lcolor(red%50)) title(7th Grade) ytitle(DID Estimates)
graph save "${output}\AppendixA9.gph",replace

********************************************************************************
*                                 Appendix A10                                 *
********************************************************************************

use "${user}\data\data.dta", clear

reghdfe chg_enrolment6 before6 before5 before4 before3 before2 after0 after1 after2 after3 before1 $yearinteracted if rural==0 & balancedpanel6==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
est store Dynamic10a
reghdfe chg_enrolment6 before6 before5 before4 before3 before2 after0 after1 after2 after3 before1 $yearinteracted if rural==1 & balancedpanel6==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
est store Dynamic10b
coefplot Dynamic10a, bylabel(Urban Girls - 6th Grade) || Dynamic10b, bylabel(Rural Girls - 6th Grade)  ||, omitted keep(before* after*) order(before6 before5 before4 before3 before2  before1 after0 after1 after2 after3) vertical recast(connect) ciopts(recast(rcap) lpattern(dash) lcolor(black%90)) lpattern(solid) mcolor(black) lcolor(black) yline(0,lcolor(gray%50)) xtitle(Treatment Period) ylabel(-1.5(.5).5,angle(90) glcolor(gs15%50)) xline(6, lcolor(red%50)) ytitle(DID Estimates) byopts(plotregion(fcolor(white)) graphregion(fcolor(white) margin(2 2 2 2))) p1(plotregion(lcolor(black) lwidth(medthin))) p2(plotregion(lcolor(black))) subtitle(, bcolor(white)) ysize(5) xsize(10)
graph save "${output}\AppendixA10.gph",replace

********************************************************************************
*                                 Appendix A11                                 *
********************************************************************************

use "${user}\data\datause.dta", clear

gen toilet=1 if shetoiletwomenfriendlytoiletinst!=.
duplicates drop schcd, force
graph bar (sum) toilet,over(shetoiletwomenfriendlytoiletinst) graphregion(color(white) margin(2 2 2 2)) b1title("Year") ytitle(Number of SHE-Toilet Construction) bar(1,color(black%60)) ylabel(, nogrid) plotregion(lcolor(black)) lintensity(0) saving("${output}\AppendixA11.gph",replace)

********************************************************************************
*                                 Appendix A12                                 *
********************************************************************************

use "${user}\data\data.dta", clear

*generate treated sample only
preserve
use "${user}\data\datause.dta", replace
keep if t_machine_year!=.
duplicates drop schcd, force
keep schcd t_machine_year
tempfile treat_sample
save `treat_sample', replace
restore

** Panel A
statsby _b[aft] _se[aft], saving("${output}\coef_placebo7.dta",replace): reghdfe chg_enrolment7 aft $yearinteracted if balancedpanel7==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
set seed 1234
forval i=1/1000{
** Step 1: generate random treatment year for treated schools
preserve
use `treat_sample', clear
gen t_year=runiformint(2013,2020)
tempfile newtreat
save `newtreat'
restore
** Step 2: merge generated treatment sample to data
preserve
merge m:1 schcd using `newtreat', keepusing(t_year) nogen
* generate new treatment dummies
drop treatment_period aft
gen treatment_period=year-t_year if !missing(t_year)
gen aft=treatment_period>=0 & gender==2 & t_machine_installed==1
statsby _b[aft] _se[aft], saving("${output}\placebo7.dta",replace): reghdfe chg_enrolment7 aft $yearinteracted if balancedpanel7==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
* append coefficients
use "${output}\coef_placebo7.dta",clear
append using "${output}\placebo7.dta"
save "${output}\coef_placebo7.dta",replace
restore
}
* create figure
preserve
use "${output}\coef_placebo7.dta", clear
drop in 1/1
kdensity _stat_1, gen(epoints densitye) xline(0,lcolor(red%50)) graphregion(color(white) margin(2 2 2 2)) plotregion(lcolor(black)) ylabel(,angle(90) glcolor(gs15%50)) lcolor(black) xlabel(-0.4(0.1)0.4) xtitle("Placebo Estimates")
summ densitye
kdensity _stat_1, xline(0,lcolor(black%50) lpattern(dash)) xline(.01088683,lcolor(black)) graphregion(color(white) margin(2 2 2 2)) plotregion(lcolor(black)) ylabel(,angle(90) glcolor(gs15%50)) lcolor(black) xlabel(-0.4(0.1)0.4) xtitle("Placebo Estimates") ytitle("Kernel Density") title("Distribution of Placebo Treatment Effects")
graph save "${output}\AppendixA12a.gph",replace
restore

** Panel B
statsby _b[aft] _se[aft], saving("${output}\coef_placebo7_2.dta",replace): reghdfe chg_enrolment7 aft $yearinteracted if balancedpanel7==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
set seed 1234
forval i=1/1000{
** Step 1: generate random sample of treated schools among untreated (494) & randomly assign treatment year
preserve
reghdfe chg_enrolment7 aft $yearinteracted if balancedpanel7==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
keep if e(sample) & t_machine_year==.
duplicates drop schcd, force
keep schcd
sample 494, count
sort schcd
gen t_year=runiformint(2013,2020)
tempfile newtreat
save `newtreat'
restore
** Step 3: merge to original dataset
preserve
merge m:1 schcd using `newtreat', keepusing(t_year)
* generate treatment period
drop treatment_period aft
drop if t_machine_installed==1
gen treatment_period=year-t_year if !missing(t_year)
gen aft=treatment_period>=0 & gender==2 & !missing(t_year)
statsby _b[aft] _se[aft], saving("${output}\placebo7_2.dta",replace): reghdfe chg_enrolment7 aft $yearinteracted if balancedpanel7==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
* append coefficients
use "${output}\coef_placebo7_2.dta",clear
append using "${output}\placebo7_2.dta"
save "${output}\coef_placebo7_2.dta",replace
restore
}
* create figure
preserve
use "${output}\coef_placebo7_2.dta", clear
drop in 1/1
kdensity _stat_1, gen(epoints densitye) xline(0,lcolor(red%50)) graphregion(color(white) margin(2 2 2 2)) plotregion(lcolor(black)) ylabel(,angle(90) glcolor(gs15%50)) lcolor(black) xlabel(-0.4(0.1)0.4) xtitle("Placebo Estimates")
summ densitye
kdensity _stat_1, xline(0,lcolor(black%50) lpattern(dash)) xline(-.00072453,lcolor(black)) graphregion(color(white) margin(2 2 2 2)) plotregion(lcolor(black)) ylabel(,angle(90) glcolor(gs15%50)) lcolor(black) xlabel(-0.5(0.1)0.5) xtitle("Placebo Estimates") ytitle("Kernel Density") title("Distribution of Placebo Treatment Effects")
graph save "${output}\AppendixA12b.gph",replace
restore

********************************************************************************
*                                 Appendix A13                                 *
********************************************************************************

use "${user}\data\data.dta", clear

reghdfe chg_enrolment1 aft $yearinteracted if balancedpanel1==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA13.doc", replace nocons nonotes label bdec(3) sdec(3) ctitle(1st-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

reghdfe chg_enrolment2 aft $yearinteracted if balancedpanel2==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA13.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(2nd-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

reghdfe chg_enrolment3 aft $yearinteracted if balancedpanel3==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA13.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(3rd-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

reghdfe chg_enrolment4 aft $yearinteracted if balancedpanel4==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA13.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(4th-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

use "${user}\data\data_a13.dta", clear
reghdfe chg_enrolment7 aft $yearinteracted if balancedpanel7==1 & gender==1 & schtype==3 & schcd!=32051200215, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA13.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(Boys 7th-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

********************************************************************************
*                                 Appendix A14                                 *
********************************************************************************

use "${user}\data\data.dta", clear

reghdfe chg_enrolment7 aft $yearinteracted if balancedpanel7==1, absorb(school_gender##c.year blockyear) vce(cluster villagecode)
outreg2 using "${output}\AppendixA14.doc", replace nocons nonotes label bdec(3) sdec(3) ctitle(1st-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

gen drop1=t_machine_installed==0 & t_distribution==1
reghdfe chg_enrolment7 aft $yearinteracted if drop1!=1 & balancedpanel7==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA14.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(1st-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

gen drop2=t_machine_installed==1 & t_machine_nostudent==0
reghdfe chg_enrolment7 aft $yearinteracted if drop2!=1 & balancedpanel7==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA14.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(1st-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

reghdfe chg_enrolment7 aft $yearinteracted if drop2!=1 & drop1!=1 & balancedpanel7==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA14.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(1st-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

********************************************************************************
*                                 Appendix A15                                 *
********************************************************************************

use "${user}\data\data.dta", clear

foreach v in conti_e funds_e schmntcgrant_e conti_r funds_r schmntcgrant_r visitscrc visitsbrc {
bysort schcd (year): replace `v'=`v'[_n+1]
gen log_`v' = log(`v' + 1) if !missing(`v')
}

reghdfe log_conti_e aft $yearinteracted if balancedpanel7==1 & c7!=., absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA15.doc", replace nocons nonotes label bdec(3) sdec(3) ctitle(1st-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool

foreach v in log_funds_e log_schmntcgrant_e log_conti_r log_funds_r log_schmntcgrant_r log_visitscrc log_visitsbrc {
reghdfe `v' aft $yearinteracted if balancedpanel7==1 & c7!=., absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA15.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(1st-Grade) keep(aft) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y)
tabschool
}

********************************************************************************
*                                 Appendix A16                                 *
********************************************************************************

use "${user}\data\data_6.dta", clear

label var bef BeforeT
label var bef AfterT

* pretrend
reghdfe a7 bef aft if balancedpanel7==1, absorb(school_gender##c.year school_year) vce(cluster schcd)
outreg2 using "${output}\AppendixA16.doc", replace nocons nonotes label bdec(3) sdec(3) ctitle(7th-Grade) keep(bef aft) addtext(School x Gender fixed effects, Y, School x Gender specific linear time trends, Y, School x year fixed effects, Y, Block x year fixed effects, N)

reghdfe a7 bef aft if balancedpanel7==1, absorb(school_gender school_year) vce(cluster schcd)
outreg2 using "${output}\AppendixA16.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(7th-Grade) keep(bef aft) addtext(School x Gender fixed effects, Y, School x Gender specific linear time trends, N, School x year fixed effects, Y, Block x year fixed effects, N)

reghdfe a7 bef aft if balancedpanel7==1, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA16.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(7th-Grade) keep(bef aft) addtext(School x Gender fixed effects, Y, School x Gender specific linear time trends, Y, School x year fixed effects, N, Block x year fixed effects, Y)

* pretrend unbalanced panel
reghdfe a7 bef aft, absorb(school_gender##c.year school_year) vce(cluster schcd)
outreg2 using "${output}\AppendixA16.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(7th-Grade) keep(bef aft) addtext(School x Gender fixed effects, Y, School x Gender specific linear time trends, Y, School x year fixed effects, Y, Block x year fixed effects, N)

reghdfe a7 bef aft, absorb(school_gender school_year) vce(cluster schcd)
outreg2 using "${output}\AppendixA16.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(7th-Grade) keep(bef aft) addtext(School x Gender fixed effects, Y, School x Gender specific linear time trends, N, School x year fixed effects, Y, Block x year fixed effects, N)

reghdfe a7 bef aft, absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA16.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(7th-Grade) keep(bef aft) addtext(School x Gender fixed effects, Y, School x Gender specific linear time trends, Y, School x year fixed effects, N, Block x year fixed effects, Y)

********************************************************************************
*                                 Appendix A17                                 *
********************************************************************************

use "${user}\data\datapc.dta", clear

preserve
local v "publicbus"
gen aft1 = aft*`v'
gen aft_n=aft*(`v'==0)
lab var aft1 "AfterT x [Indicator=1]"
lab var aft_n "AfterT x [Indicator=0]"
reghdfe chg_enrolment7 aft1 aft_n $yearinteracted if balancedpanel7==1 , absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA17.doc", replace nocons nonotes label bdec(3) sdec(3) ctitle(7th-Grade) keep(aft*) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y) 
test aft1 = aft_n
tab `v' t_machine_installed if year==2013 & e(sample) 
restore

preserve
local v "highway"
gen aft1 = aft*`v'
gen aft_n=aft*(`v'==0)
lab var aft1 "AfterT x [Indicator=1]"
lab var aft_n "AfterT x [Indicator=0]"
reghdfe chg_enrolment7 aft1 aft_n $yearinteracted if balancedpanel7==1 , absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA17.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(7th-Grade) keep(aft*) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y) 
test aft1 = aft_n
tab `v' t_machine_installed if year==2013 & e(sample) 
restore

preserve
local v "chc"
gen aft1 = aft*`v'
gen aft_n=aft*(`v'==0)
lab var aft1 "AfterT x [Indicator=1]"
lab var aft_n "AfterT x [Indicator=0]"
reghdfe chg_enrolment7 aft1 aft_n $yearinteracted if balancedpanel7==1 , absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA17.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(7th-Grade) keep(aft*) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y) 
test aft1 = aft_n
tab `v' t_machine_installed if year==2013 & e(sample) 
restore

preserve
local v "phc"
gen aft1 = aft*`v'
gen aft_n=aft*(`v'==0)
lab var aft1 "AfterT x [Indicator=1]"
lab var aft_n "AfterT x [Indicator=0]"
reghdfe chg_enrolment7 aft1 aft_n $yearinteracted if balancedpanel7==1 , absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA17.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(7th-Grade) keep(aft*) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y) 
test aft1 = aft_n
tab `v' t_machine_installed if year==2013 & e(sample) 
restore

preserve
local v "fwc"
gen aft1 = aft*`v'
gen aft_n=aft*(`v'==0)
lab var aft1 "AfterT x [Indicator=1]"
lab var aft_n "AfterT x [Indicator=0]"
reghdfe chg_enrolment7 aft1 aft_n $yearinteracted if balancedpanel7==1 , absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA17.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(7th-Grade) keep(aft*) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y) 
test aft1 = aft_n
tab `v' t_machine_installed if year==2013 & e(sample) 
restore

preserve
local v "mcw"
gen aft1 = aft*`v'
gen aft_n=aft*(`v'==0)
lab var aft1 "AfterT x [Indicator=1]"
lab var aft_n "AfterT x [Indicator=0]"
reghdfe chg_enrolment7 aft1 aft_n $yearinteracted if balancedpanel7==1 , absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA17.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(7th-Grade) keep(aft*) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y) 
test aft1 = aft_n
tab `v' t_machine_installed if year==2013 & e(sample) 
restore

preserve
local v "rpm"
gen aft1 = aft*`v'
gen aft_n=aft*(`v'==0)
lab var aft1 "AfterT x [Indicator=1]"
lab var aft_n "AfterT x [Indicator=0]"
reghdfe chg_enrolment7 aft1 aft_n $yearinteracted if balancedpanel7==1 , absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA17.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(7th-Grade) keep(aft*) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y) 
test aft1 = aft_n
tab `v' t_machine_installed if year==2013 & e(sample) 
restore

preserve
local v "courier"
gen aft1 = aft*`v'
gen aft_n=aft*(`v'==0)
lab var aft1 "AfterT x [Indicator=1]"
lab var aft_n "AfterT x [Indicator=0]"
reghdfe chg_enrolment7 aft1 aft_n $yearinteracted if balancedpanel7==1 , absorb(school_gender##c.year blockyear) vce(cluster schcd)
outreg2 using "${output}\AppendixA17.doc", append nocons nonotes label bdec(3) sdec(3) ctitle(7th-Grade) keep(aft*) addtext(School characteristics x year controls, Y, School fixed effects, Y, School-specific linear time trends, Y, Block x year fixed effects, Y) 
test aft1 = aft_n
tab `v' t_machine_installed if year==2013 & e(sample) 
restore

********************************************************************************
*                                 Appendix A18                                 *
********************************************************************************

use "${user}\data\data.dta", clear

* gen variables
egen totaltch=rowtotal(tch_female tch_male tch_nr) if caste=="TOTAL"
gen Femaleshare=tch_female/totaltch
gen Maleshare=tch_male/totaltch
gen Professionalshare=tchwithprof/totaltch
gen Graduateshare=gradabove/totaltch

* create figures
foreach v in Female Male Professional Graduate {
reghdfe `v'share before6 before5 before4 before3 before2 after0 after1 after2 after3 after4 before1  $yearinteracted if balancedpanel7==1 & c7!=., absorb(school_gender##c.year blockyear) vce(cluster schcd)
est store D`v'
coefplot D`v', omitted keep(before* after*) order(before6 before5 before4 before3 before2  before1 after0 after1 after2 after3 after4) vertical recast(connect) ciopts(recast(rcap) lpattern(dash) lcolor(black%90)) lpattern(solid) mcolor(black) lcolor(black) yline(0,lcolor(gray%50)) xtitle(Treatment Period) ylabel(,angle(90) glcolor(gs15%50)) scale(titlegap(.5) outergap(.5)) graphregion(color(white) margin(2 2 2 2)) plotregion(lcolor(black)) xline(6, lcolor(red%50)) title(Share of `v' Teacher) ytitle(DID Estimates)
graph save "${output}\A`v'.gph",replace
}

graph combine AFemale.gph AMale.gph AGraduate.gph AProfessional.gph, row(2) col(2) ysize(10) xsize(20) commonscheme graphregion(color(white)) saving("${output}\AppendixA18.gph", replace)

foreach v in Female Male Professional Graduate {
erase "${output}\A`v'.gph"
}
















